/**
 * Copyright (C) 2018-2022
 * All rights reserved, Designed By www.yixiang.co
 * 注意：
 * 本软件为www.yixiang.co开发研制
 */
package com.avic.modules.system.rest;

import com.avic.config.DataScope;
import com.avic.dozer.service.IGenerator;
import com.avic.modules.system.domain.Dept;
import com.avic.modules.system.service.DeptService;
import com.avic.modules.system.service.dto.DeptDto;
import com.avic.modules.system.service.dto.DeptQueryCriteria;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
 * @author zhangzikang
 * @date 2022-08-14
 */
@RestController
@Api(tags = "系统：部门管理")
@RequestMapping("/api/dept")
public class DeptController {

    private final DeptService deptService;

    private final DataScope dataScope;

    private final IGenerator generator;

    public DeptController(DeptService deptService, DataScope dataScope, IGenerator generator) {
        this.deptService = deptService;
        this.dataScope = dataScope;
        this.generator = generator;
    }

    @ApiOperation("导出部门数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('admin','dept:download')")
    public void download(HttpServletResponse response, DeptQueryCriteria criteria) throws IOException {
        deptService.download(generator.convert(deptService.queryAll(criteria), DeptDto.class), response);
    }

    @ApiOperation("查询部门")
    @GetMapping
    @PreAuthorize("@el.check('user:list','admin','dept:list', 'job:list')")
    public ResponseEntity<Object> getDepts(DeptQueryCriteria criteria) {
        // 数据权限
        criteria.setIds(dataScope.getDeptIds());
        if(criteria.getName()!="" && criteria.getName()!= null){
            criteria.setDeptName(criteria.getName());
        }
        List<DeptDto> deptDtos = generator.convert(deptService.queryAll(criteria), DeptDto.class);
        for(DeptDto deptDto:deptDtos){
            deptDto.setIsDisabled(deptDto.getEnabled()?false:true);
        }
        if(criteria.getDeptName()!=null||criteria.getCreateTime()!=null){
            Map<String, Object> map = new HashMap<>(2);
            map.put("totalElements", deptDtos.size());
            map.put("content", deptDtos);

            return new ResponseEntity<>(map, HttpStatus.OK);
        }else {
            return new ResponseEntity<>(deptService.buildTree(deptDtos), HttpStatus.OK);
        }
    }

    @ApiOperation("新增部门")
    @PostMapping
    @PreAuthorize("@el.check('admin','dept:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody Dept resources) {
        deptService.add(resources);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation("修改部门")
    @PutMapping
    @PreAuthorize("@el.check('admin','dept:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody Dept resources) {
        deptService.updateDept(resources);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation("删除部门")
    @DeleteMapping
    @PreAuthorize("@el.check('admin','dept:del')")
    public ResponseEntity<Object> delete(@RequestBody Set<Long> ids) {
        List<Long> deptIds = new ArrayList<>();
        for (Long id : ids) {
            deptIds.add(id);
        }

        deptService.delDepts(deptIds);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
